package com.bluesky.plum.domain.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bluesky.plum.domain.City;
import com.bluesky.plum.domain.Color;
import com.bluesky.plum.domain.Person;
import com.bluesky.plum.domain.Sex;
import com.bluesky.plum.domain.northwind.Employee;
import com.bluesky.plum.domain.northwind.EmployeeRepository;
import com.bluesky.plum.domain.repository.impl.EnumRepository;
import com.bluesky.plum.domain.repository.impl.ListRepository;

/**
 * all entity repository should register here
 * 
 * @author HZ00260
 * 
 */
public class RepositoryFactory {
	private static Map<Class, Repository> map = new HashMap<Class, Repository>();
	static {
		//map.put(Person.class, new PersonRepository());

		map.put(Sex.class, new EnumRepository<Sex>(Sex.class));

		map.put(City.class, new ListRepository<City>(City.allCities));

		map.put(Color.class, new EnumRepository<Sex>(Color.class));

		//map.put(Employee.class, new EmployeeRepository());
	}

	public static void register(Class cls, Repository repo){
		map.put(cls, repo);
	}
	
	public static Repository getRepository(Class clazz) {
		Repository repo = map.get(clazz);
		if (repo == null)
			throw new RuntimeException("class " + clazz.getSimpleName()
					+ " do not registered.");
		else
			return repo;
	}

	public static void main(String[] args) {
		List l = RepositoryFactory.getRepository(Sex.class).getAll();
		for (Object o : l)
			System.out.println(o);
	}

	public static Repository repositoryWapper(Object obj) {
		if (obj instanceof Repository)
			return (Repository) obj;
		else if (obj instanceof List)
			return new ListRepository((List) obj);
		else
			throw new RuntimeException("unrecognized collection");

	}

	public static boolean equals(Object obj1, Object obj2) {
		if (!obj1.getClass().equals(obj2.getClass()))
			return false;

		Repository repo1 = getRepository(obj1.getClass());
		return repo1.equals(obj1, obj2);
	}

}
